package com.ywj.collection.MyMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo04 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("student1", "zhangsan");
        map.put("student2", "lisi");
        map.put("student3", "wangwu");
        map.put("student4", "zhaoliu");

        //方式一：获取所有键的集合。用keySet()方法实现
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            //根据键去找值。用get(Object key)方法实现
            String value = map.get(key);
            System.out.println(key + "," + value);
        }

        System.out.println("~~~~~");

        //方式二：获取所有键值对对象的集合
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            //根据键值对对象获取键和值
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "," + value);
        }
    }
}
